//
//  RNTextView.m
//  iOS UI Sample Code
//
//  Created by 규성 이 on 12. 2. 20..
//  Copyright (c) 2012년 kkamci77@gmail.com. All rights reserved.
//

#import "RNTextView.h"

@implementation RNTextView

@synthesize tvInput, bClear, ivBackground;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [self initUIWithFrame:frame];
    }
    return self;
}

-(void)setTextViewDelegate:(id<UITextViewDelegate>)dg
{
    [self.tvInput setDelegate:dg];
}

-(void)setTagOfTextView:(NSInteger)tag
{
    [self.tvInput setTag:tag];
}

-(void)addButtonTarger:(id)object action:(SEL)selector forControlEvents:(UIControlEvents) event
{
    [self.bClear addTarget:object action:selector forControlEvents:event];
}

-(void)initUIWithFrame:(CGRect)frame
{
    // Minimum Height 30.0f
    // Minimum Width 15.0f
    self.ivBackground = [[[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, frame.size.width, frame.size.height)] autorelease];
    [self.ivBackground setImage:[UIImage imageNamed:@"text_input.png"]];
    
    [self setBackgroundColor:UIColor.whiteColor];
    self.tvInput = [[UITextView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, frame.size.width-30.0f, frame.size.height)];
    [self.tvInput setScrollEnabled:NO];
    [self.tvInput setFont:[UIFont systemFontOfSize:13.0f]];
    self.bClear = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.bClear setFrame:CGRectMake(5.0f + self.tvInput.frame.size.width, (frame.size.height/2)-10.0f, 20.0f, 20.0f)];
    [self.bClear addTarget:self action:@selector(clearButtonAction) forControlEvents:UIControlEventTouchUpInside];
//    [self.bClear setBackgroundColor:UIColor.redColor];
    [self.ivBackground setContentStretch:CGRectMake(0.5f, 0.5f, 0.0f, 0.0f)];
    [self.tvInput setBackgroundColor:UIColor.clearColor];
    [self.bClear setBackgroundImage:[UIImage imageNamed:@"ic_close_n.png"] forState:UIControlStateNormal];
    [self.bClear setBackgroundImage:[UIImage imageNamed:@"ic_close_p.png"] forState:UIControlStateHighlighted];
    [self.bClear setShowsTouchWhenHighlighted:YES];
    [self addSubview:self.ivBackground];
    [self addSubview:self.tvInput];
    [self addSubview:self.bClear];
}

-(void)clearButtonAction
{
    [self.tvInput setText:@""];
}

-(void)layoutSubviews
{
    self.ivBackground.frame = CGRectMake(0.0f, 0.0f, self.frame.size.width, self.frame.size.height);
    self.tvInput.frame = CGRectMake(0.0f, 0.0f, self.frame.size.width-30.0f, self.frame.size.height);
    [self.bClear setFrame:CGRectMake(5.0f + self.tvInput.frame.size.width, (self.frame.size.height/2)-10.0f, 20.0f, 20.0f)];
    //    [self.bClear setBackgroundColor:UIColor.redColor];
}

- (CGSize)getContentSizeofTextView
{
    return [self.tvInput contentSize];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/
- (void)dealloc {
    [tvInput release];
    [bClear release];
    [ivBackground release];
    [super dealloc];
}
@end
